package com.lagou.service.impl;

import com.lagou.service.EmailService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

@Service
public class EmailServiceImpl implements EmailService {

    private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);

    static {
        System.setProperty("mail.mime.splitlongparameters","false");
        System.setProperty("mail.mime.charset","UTF-8");
    }

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from;

    @Override
    public boolean findDefaultResumeByUserId(String email, String code) {
        MimeMessage message = sender.createMimeMessage();

        try {
            //true 表示要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);

            helper.setTo(new String[]{email});
            helper.setBcc("729148553@qq.com");
            helper.setSubject("注册验证码");
            helper.setText("您本次操作验证码为："+code, true);

            sender.send(message);
            logger.info("邮件已发送! 目标邮箱  {}  code  {} ", email, code);
            return true;
        } catch (Exception e) {
            logger.error("发送邮件发生异常!",e);
            return false;
        }
    }
}
